
#include "Component/LineReplicationGraphComponent.h"

#include "LineReplicationGraphObject.h"

ULineReplicationGraphComponent* ULineReplicationGraphComponent::Get(const AActor* Owner)
{
	if(!IsValid(Owner))
	{
		return nullptr;
	}
	if(auto Comonent = Owner->GetComponentByClass(ULineReplicationGraphComponent::StaticClass() ) )
	{
		return Cast<ULineReplicationGraphComponent>(Comonent);
	}
	return nullptr;
}

ULineReplicationGraphComponent* ULineReplicationGraphComponent::Create(AActor* Owner)
{
	ULineReplicationGraphComponent* Component = NewObject<ULineReplicationGraphComponent>(Owner);
	Component->RegisterComponent();
	return Component;
}

void ULineReplicationGraphComponent::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
}

void ULineReplicationGraphComponent::SetReplicationType(ELineReplicationType InType)
{
	ReplicationType = InType;
}

void ULineReplicationGraphComponent::SetLine(const FName& LineName)
{
	CurrentLine = LineName;
}

void ULineReplicationGraphComponent::BeginPlay()
{
	Super::BeginPlay();

	if(auto ReplicationGraph =  ULineReplicationGraph::Get(this) )
	{
		ReplicationGraph->AddNetworkActor(GetOwner());
	}
}

bool ULineReplicationGraphComponent::CanBeVisible_Implementation(const TSet<FName>& VisibleLines)
{
	return VisibleLines.Contains(CurrentLine);
}

void ULineReplicationGraphComponent::OnRemoveFromNetwork()
{
	
}

void ULineReplicationGraphComponent::OnAddToNetwork()
{
	
}

FNewReplicatedActorInfo& ULineReplicationGraphComponent::GetNewReplicatedActorInfo()
{
	if(!NewReplicatedActorInfo)
	{
		NewReplicatedActorInfo = new FNewReplicatedActorInfo(GetOwner());
		NewReplicatedActorInfo->Class = GetOwner()->GetClass();
	}
	return *NewReplicatedActorInfo;
}

const FClassReplicationInfo& ULineReplicationGraphComponent::GetClassReplicationInfo() const
{
	if(!ReplicationInfo)
	{
		static FClassReplicationInfo s_Empty;
		return s_Empty;
	}
	return ReplicationInfo->Settings;
}

void ULineReplicationGraphComponent::SetReplicationInfo(FGlobalActorReplicationInfo& InReplicationInfo)
{
	if(!ReplicationInfo)
	{
		ReplicationInfo = new FGlobalActorReplicationInfo(InReplicationInfo.Settings);
	}
	ReplicationInfo->LastPreReplicationFrame = InReplicationInfo.LastFlushNetDormancyFrame;
	ReplicationInfo->ForceNetUpdateFrame = InReplicationInfo.ForceNetUpdateFrame;
	ReplicationInfo->WorldLocation = InReplicationInfo.WorldLocation;
	ReplicationInfo->bWantsToBeDormant = InReplicationInfo.bWantsToBeDormant;
	ReplicationInfo->bSwapRolesOnReplicate = InReplicationInfo.bSwapRolesOnReplicate;
	ReplicationInfo->Settings = InReplicationInfo.Settings;
	ReplicationInfo->LastFlushNetDormancyFrame = InReplicationInfo.LastFlushNetDormancyFrame;
}

void ULineReplicationGraphComponent::SetNewReplicatedActorInfo(const FNewReplicatedActorInfo& InNewReplicatedActorInfo)
{
	if(!NewReplicatedActorInfo)
	{
		NewReplicatedActorInfo = new FNewReplicatedActorInfo(InNewReplicatedActorInfo.Actor);
	}
	NewReplicatedActorInfo->Class = InNewReplicatedActorInfo.Class;
}

void ULineReplicationGraphComponent::UpdateLocationCell(ULRG_Node_GridCell* Cell)
{
	LocationCell = Cell;
}